package com.lyw.controller;


import com.lyw.pojo.User;
import com.lyw.service.UserService;
import com.lyw.util.CodeMsg;
import com.lyw.util.Md5;
import com.lyw.util.Result;
import com.lyw.util.StringUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/findUser")
    public List<User> findUser(){
        return userService.findUser();
    }

    @PostMapping("/zhuce")
    public void addUser(@RequestBody User user){
        userService.addUser(user);
    }

    @DeleteMapping("/delUser")
    public void delUser(Long id){
        userService.delUser(id);
    }

    @GetMapping("/findById")
    public User findById(Long id){
        return userService.findById(id);
    }

    @PutMapping("/updUser")
    public void updUser(@RequestBody User user){
        userService.updUser(user);
    }

    /*@PostMapping("zhuce1")
    public Result<User> zhuce(@RequestBody User user) {

        Map map = new HashMap();

        String s = SixMath();
        map.put("phone",user.getPhone());
        map.put("code",s);
        rabbitTemplate.convertAndSend("sms",map);
        return userService.zhuce1(user);
    }*/


    @GetMapping("/login")
    public User login(
            @RequestParam(name = "username") String username,
            @RequestParam(name = "password") String password,
            HttpSession session
    ){
        //salt的长度
        String salt = StringUtil.getRandomString(6);
        String s = Md5.GetMD5Code(Md5.GetMD5Code(password+salt));
        System.out.println(s);
        User user = userService.login(username,s);
        if(user != null){
            session.setAttribute("loginUser",username);
            return user;
        }else {
            session.invalidate();
            return null;
        }
    }



    @GetMapping(value = "/sendsms/{mobile}")
    public String sendsms(@PathVariable String mobile, HttpServletRequest request) {
        userService.sendSms(mobile,request);
        return  "消息发送成功";
    }


   /* public Result send(String phone, String code) {
        String host = "https://smssend.shumaidata.com";
        String path = "/sms/send";
        String method = "POST";
        String appcode = "df96df83725845b8863d2f541fbc933f";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("receive",phone);
        querys.put("tag",code);
        querys.put("templateId", "M09DD535F4");
        Map<String, String> bodys = new HashMap<String, String>();


        try {
            *//**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             *//*
            HttpResponse response = SendMsg.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));
            return    Result.success(CodeMsg.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.error(CodeMsg.mistake);

    }


    public String SixMath(){
        Random random = new Random();
        String result="";
        for (int i=0;i<6;i++)
        {
            result+=random.nextInt(10);
        }
        return result;
    }
    //生成一个六位随机数*/




}
